package com.zhangfei.thread;

public class WaitNotifyExample {
    private static final Object lock = new Object();
    private static boolean flag = false;

    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            synchronized (lock) {
                try {
                    while (!flag) {
                        System.out.println("Thread 1 is waiting...");
                        lock.wait();
                    }
                    System.out.println("Thread 1 is notified and running...");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread thread2 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("Thread 2 is running...");
                flag = true;
                lock.notify();
            }
        });

        thread1.start();
        thread2.start();
    }
}
